<?php
//自动生成,请根据需要修改
namespace app\topic\controller;
use app\common\controller\Base;
use think\Db;
/**
* @title 回复页面（模块06-04）
* @description 回复说明
* @group 帖子（06）
* @header name:model_id require:0 default:06-04 desc:模块
* @groupremark 已使用错误码：1-06-04-0001 校验失败 1-06-04-0002 添加失败 1-06-04-0003 编辑校验失败 1-06-04-0004 编辑失败 1-06-04-0005 获取详情失败  1-06-04-0006 获取列表失败 1-06-04-0007获取单个详情失败 1-06-04-0008 删除失败
*/
class Reply extends Base{
		//测试
	public function test(){
		return 'test 111';
	}
	
	/**
	* @title 添加回复
	* @description 添加回复
	* @author 李锐志
	* @url /topic/reply/add
	* @method *
	*
	* @header name:token require:1 default: desc:token
	*
	* @param name:topic_id type:int require:0 other: desc:帖子id
	* @param name:comment_id type:int require:0 other: desc:评论id
	* @param name:user_id type:int require:0 other: desc:用户id
	* @param name:argument type:text require:0 other: desc:论点
	* @param name:saytext type:text require:0 other: desc:依据
	* @param name:like_num type:int require:0 other: desc:点赞数量
	* @param name:img_url type:text require:0 other: desc:图片路径
	* @param name:create_time type:int require:0 other: desc:创建时间
	* @param name:reply_user_id type:int require:0 other: desc:被回复用户id
	*@return data:回复@!
	* @data 	id:id	topic_id:帖子id	comment_id:评论id	user_id:用户id	argument:论点	saytext:依据	like_num:点赞数量	img_url:图片路径	create_time:创建时间	reply_user_id:被回复用户id
	*@return error_id:1-06-04-0001 校验失败  1-06-04-0002 添加失败
	*/
	public function add(){
		$data=input();
		/**
		*$validate = new \app\topic\validate\Reply();
		*$validate->scene('add');
		*$result = $validate->batch(true)->check($data);
		*if ($result !== true) {
		*$msg['msg']='校验失败';
		*$msg['validate']=$validate->getError();
		*$msg['error_id']='1-06-04-0001';
		*return $this->errorJson($msg);
		*}
		**/
		$model=new \app\topic\model\Reply();
		$flag = $model->save($data);
		if($flag){
			$msg['msg']='添加成功';
			$msg['data']=$model->get($model['id']);
			return $this->successJson($msg);
		}else{
			$msg['error_id']='1-06-04-0002';
			$msg['msg']='添加失败';
			return $this->errorJson($msg);
		}
	}
		/**
	* @title 编辑回复
	* @description 编辑回复
	* @author 李锐志
	* @url /topic/reply/edit
	* @method *
	*
	* @header name:token require:1 default: desc:token
	*
		* @param name:id type:int require:0 other: desc:id
		* @param name:topic_id type:int require:0 other: desc:帖子id
		* @param name:comment_id type:int require:0 other: desc:评论id
		* @param name:user_id type:int require:0 other: desc:用户id
		* @param name:argument type:text require:0 other: desc:论点
		* @param name:saytext type:text require:0 other: desc:依据
		* @param name:like_num type:int require:0 other: desc:点赞数量
		* @param name:img_url type:text require:0 other: desc:图片路径
		* @param name:create_time type:int require:0 other: desc:创建时间
		* @param name:reply_user_id type:int require:0 other: desc:被回复用户id
		*@return data:回复@!
	* @data 	id:id	topic_id:帖子id	comment_id:评论id	user_id:用户id	argument:论点	saytext:依据	like_num:点赞数量	img_url:图片路径	create_time:创建时间	reply_user_id:被回复用户id
	*@return error_id:1-06-04-0003 校验失败 1-06-04-0004 修改失败
	*/
	public function edit(){
		$data=input();
		$validate = new \app\topic\validate\Reply();
		$validate->scene('edit');
		$result = $validate->batch(true)->check($data);
		if ($result !== true) {
			$msg['msg']='校验失败';
			$msg['validate']=$validate->getError();
			$msg['error_id']='1-06-04-0003';
			return $this->errorJson($msg);
		}
		$model=new \app\topic\model\Reply();
		$flag = $model->edit($data);
		if($flag!==false){
			$msg['msg']='修改成功';
			$msg['data']=$model->get($data['id']);
			return $this->successJson($msg);
		}else{
			$msg['error_id']='1-06-04-0004';
			$msg['msg']='修改失败';
			return $this->errorJson($msg);
		}
	}
		
	/**
	* @title 删除回复
	* @description 删除回复
	* @author 李锐志
	* @url /topic/reply/delete
	* @method *
	*
	* @param name:token require:1 default: desc:token
	*
	* @param name:id type:int|array require:1 other: desc:要删除的id
	*@return error_id:1-06-04-0008 删除失败
	*/
	public function delete(){
		$id=input('id');
		$model=new \app\topic\model\Reply();
		$flag = $model->destroy($id);
		if($flag){
			$msg['msg']='删除成功';
			return $this->successJson($msg);
		}else{
			$msg['msg']='删除失败';
			$msg['error_id']='1-06-04-0008';
			return $this->errorJson($msg);
		}
	}
	    
    /**
    * @title 单个查询回复
    * @description 单个查询回复
    * @author 李锐志
    * @url /topic/reply/info
    * @method *
    *
    * @param name:token require:1 default: desc:token
    *
        * @param name:id type:int require:0 other: desc:id
        * @param name:topic_id type:int require:0 other: desc:帖子id
        * @param name:comment_id type:int require:0 other: desc:评论id
        * @param name:user_id type:int require:0 other: desc:用户id
        * @param name:argument type:text require:0 other: desc:论点
        * @param name:saytext type:text require:0 other: desc:依据
        * @param name:like_num type:int require:0 other: desc:点赞数量
        * @param name:img_url type:text require:0 other: desc:图片路径
        * @param name:create_time type:int require:0 other: desc:创建时间
        * @param name:reply_user_id type:int require:0 other: desc:被回复用户id
        *@return data:回复@!
    * @data 	id:id	topic_id:帖子id	comment_id:评论id	user_id:用户id	argument:论点	saytext:依据	like_num:点赞数量	img_url:图片路径	create_time:创建时间	reply_user_id:被回复用户id
    *@return error_id:1-06-04-0005 校验失败 1-06-04-0007 获取失败
    */
    public function info(){
        $model=new \app\topic\model\Reply();
        $Fields=$model->getTableFields();
        $data=\think\facade\Request::only($Fields);
        /**
        *$validate = new \app\topic\validate\Reply();
        *$validate->scene('info');
        *$result = $validate->batch(true)->check($data);
        */
        if (!$data) {
            $msg['msg']='必须有参数';
            $msg['error_id']='1-06-04-0005';
            return $this->errorJson($msg);
        }
        $model=new \app\topic\model\Reply();
        $info = $model->where($data)->find();
        if($info){
            $msg['msg']='获取成功';
            $msg['data']=$info;
            return $this->successJson($msg);
        }else{

            $msg['msg']='获取失败';
            $msg['error_id']='1-06-04-0007';
            return $this->errorJson($msg);
        }
    }
		
	/**
	* @title 查询列表回复
	* @description 查询列表回复
	* @author 宋晓文
	* @url /topic/reply/get_list
	* @method *
	* @param name:token require:1 default: desc:token
	* @param name:page require:1 default: desc:第几页
	* @param name:pagesize require:1 default: desc:每页数
	* @return list:回复@
	* @list 	id:id	topic_id:帖子id	comment_id:评论id	user_id:用户id	argument:论点	saytext:依据	like_num:点赞数量	img_url:图片路径	create_time:创建时间	reply_user_id:被回复用户id
	*@return error_id:1-06-04-0006 校验失败
	*/
	public function get_list(){
	    $message = input();

		$model=new \app\topic\model\Reply();
		$Fields=$model->getTableFields();
		$data=\think\facade\Request::only($Fields);

        $where = array();
        foreach($data as $k=>$v){
            if($v===0){
                continue;
            }
            if ($v == null){
                unset($data[$k]);
            }
        }
        foreach ($data as $k => $v){
            if ($k === 'create_time') {
                array_push($where, [$k, '>=', ceil($data[$k][0] / 1000)]);
                array_push($where, [$k, '<=', ceil($data[$k][1] / 1000)]);
                continue;
            }
            if ($k === 'id' || $k === 'topic_id' || $k === 'user_id' || $k === 'comment_id' || $k === 'user_id' || $k === 'like_num'){
                array_push($where,[$k,'=',$data[$k]]);
                continue;
            }
            if (isset($data[$k]) && $data[$k] && !empty($data[$k])){
                array_push($where,[$k,'like',"%{$data[$k]}%"]);
            }
        }

		$list = $model->where($where)->order('id','desc')->paginate();

        foreach ($list as $k=>$v){
            $topic_id = $v['topic_id'];
            $user_id = $v['user_id'];

            $topic = Db::name('topic')->where('id',$topic_id)->find();
            $user= get_curl('user','/user/info',['token'=>$message['token'],'id'=>$user_id]);
            if ($user['status']!=false){
                $list[$k]['nickname']=$user['data']['nickname'];
            }else{
                $list[$k]['nickname']="";
            }
            $list[$k]['topic_title']=$topic['title'];
        }


        $msg['list']=$list;
		return $this->successJson($msg);
	}
}
